package com.jsb.laundry.service;

import com.jsb.laundry.common.enums.BusinessType;
import com.jsb.laundry.common.utils.JSBUtil;
import com.jsb.laundry.model.entity.CapitalAccount;
import com.jsb.laundry.model.entity.CapitalAccountLog;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class CapitalAccountServiceImpl {

    private static final Logger LOGGER = LoggerFactory.getLogger(CapitalAccountServiceImpl.class);
    @Resource
    private UserLotteryService userLotteryService;


    /**
     * 新增已结算记录
     *
     * @param accountId
     * @param type
     * @param remark
     * @param relatedNo
     * @param amount
     * @param businessType
     * @param deposit
     * @param depositFreeze
     * @param withdraw
     * @param withdrawFreeze
     * @param score
     * @param accountAmount
     */
    public int instertAccountlog(
            String accountId,
            String type,
            String remark,
            String relatedNo,
            int amount,
            String businessType,
            int deposit,
            int depositFreeze,
            int withdraw,
            int withdrawFreeze,
            String score,
            int accountAmount
    ) {

        CapitalAccountLog capitalAccountLog = new CapitalAccountLog();
        String uuid = JSBUtil.generateUUID();
        capitalAccountLog.setSerialNum(uuid);//流水号
        capitalAccountLog.setAccountId(accountId);//账号id（capital_account表里的id）
        capitalAccountLog.setType(type);//类型
        capitalAccountLog.setRemark(remark);//备注
        capitalAccountLog.setRelatedNo(relatedNo);//关联订单号
        capitalAccountLog.setAmount(amount);//发生金额
        capitalAccountLog.setBusinessType(businessType);//业务类型
        capitalAccountLog.setDeposit(deposit);//可提现金额
        capitalAccountLog.setDepositFreeze(depositFreeze);//待结算金额
        capitalAccountLog.setWithdraw(withdraw);//提现金额
        capitalAccountLog.setWithdrawFreeze(withdrawFreeze);//提现中金额
        capitalAccountLog.setScore(score);//积分
        capitalAccountLog.setAccountAmount(accountAmount);//账户余额
        int addResult = userLotteryService.capitalAccountLogadd(capitalAccountLog);
        if (addResult == 0) {
            LOGGER.info("OrderSplittingServiceImpl:instertAccountUnstlog, instert failed, capitalAccountLog:{}", capitalAccountLog);
        }
        return addResult;
    }

    /**
     * 抽奖奖励结算
     * @param money
     * @param userId
     * @return
     */
    public int lotteryAwardSettlement(int money,String userId,String orderId){
        //查询本账户的资金信息
        CapitalAccount systemAccountInfo = userLotteryService.capitalAccountInfo("jsb", "5");
        //公司outAmount
        int sysAccountDepositSubResult = userLotteryService.updateDepositSub(money, systemAccountInfo.getId());
        if (sysAccountDepositSubResult == 0){
            LOGGER.info("CapitalAccountServiceImpl:lotteryAwardSettlement, systemAccount updateDepositSub failed, orderId:{}, userId:{}, amount:{}, systemAccountInfo:{}, ",
                    orderId, userId, money, systemAccountInfo);
            return 0;
        }
        //加日志
        systemAccountInfo = userLotteryService.capitalAccountInfo("jsb", "5");
        int sysAccountAddLogResult = instertAccountlog(systemAccountInfo.getId(), "out", "用户抽奖奖励:"+orderId, orderId, money,
                BusinessType.rewards_rewards, (int) systemAccountInfo.getDeposit(), (int) systemAccountInfo.getDepositFreeze(),
                (int) systemAccountInfo.getWithdraw(), (int) systemAccountInfo.getWithdrawFreeze(),
                systemAccountInfo.getScore(), (int) systemAccountInfo.getAccountAmount());
        if (sysAccountAddLogResult == 0){
            LOGGER.info("CapitalAccountServiceImpl:lotteryAwardSettlement, systemAccount instertAccountlog failed, orderId:{}, userId:{}, amount:{}, systemAccountInfo:{}, ",
                    orderId, userId, money, systemAccountInfo);
            return 0;
        }
        //用户
        CapitalAccount userAccountInfo = userLotteryService.capitalAccountInfo(userId, "1");
        //更新用户可提现金额 in
        int userAccountDepositAddResult = userLotteryService.updateDepositAdd(money,userAccountInfo.getId());
        if (userAccountDepositAddResult == 0){
            LOGGER.info("CapitalAccountServiceImpl:lotteryAwardSettlement, userAccount updateDepositAdd failed, orderId:{}, userId:{}, amount:{}, systemAccountInfo:{}, ",
                    orderId, userId, money, systemAccountInfo);
            return 0;
        }
        //加日志
        userAccountInfo = userLotteryService.capitalAccountInfo(userId, "1");
        int userAccountAddLogResult = instertAccountlog(userAccountInfo.getId(), "in", "用户抽奖奖励:"+orderId, orderId, money,
                BusinessType.rewards_rewards, (int) userAccountInfo.getDeposit(), (int) userAccountInfo.getDepositFreeze(),
                (int) userAccountInfo.getWithdraw(), (int) userAccountInfo.getWithdrawFreeze(),
                userAccountInfo.getScore(), (int) userAccountInfo.getAccountAmount());
        if (userAccountAddLogResult == 0){
            LOGGER.info("CapitalAccountServiceImpl:lotteryAwardSettlement, userAccount instertAccountlog failed, orderId:{}, userId:{}, amount:{}, systemAccountInfo:{}, ",
                    orderId, userId, money, systemAccountInfo);
            return 0;
        }
        return 1;
    }

}
